package com.wjm.androidnetproclient.okhttp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wjm.androidnetproclient.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class OKHttpActivity extends AppCompatActivity {
    private static final String TAG = "OKHttpActivity";
    private EditText et_spec;
    private Button btn_OKHttp_get;
    private Button btn_OKHttp_get_par;
    private Button btn_OKHttp_post;
    private Button btn_OKHttp_post_head;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
        findViewById();
    }

    private void findViewById() {
        et_spec = findViewById(R.id.et_spec);

        //get空参数
        btn_OKHttp_get = findViewById(R.id.btn_OKHttp_get);
        btn_OKHttp_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                OKHttpThread_Get();
                OKHttp_Get1();//okhttp本来就自已开了子线程不用去开线程了
            }
        });

        //get带参数
        btn_OKHttp_get_par = findViewById(R.id.btn_OKHttp_get_par);
        btn_OKHttp_get_par.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKHttpThread_Get_Par();
            }
        });

        //Post
        btn_OKHttp_post = findViewById(R.id.btn_OKHttp_post);
        btn_OKHttp_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKHttpThread_Post();
            }
        });

        //post带头参数
        btn_OKHttp_post_head = findViewById(R.id.btn_OKHttp_post_head);
        btn_OKHttp_post_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKHttpThread_Post_Head();
            }
        });


        tv_result = findViewById(R.id.tv_result);
    }

    private void OKHttpThread_Post_Head() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OKHttp_Post_Head();
            }
        }).start();
    }

    private void OKHttpThread_Post() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OKHttp_Post();
            }
        }).start();
    }

    private void OKHttp_Post_Head() {
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

        try {
            String url = et_spec.getText().toString().trim();

            OkHttpClient client = new OkHttpClient();

//        String post(String url, String json) throws IOException {
            String json = new String();
            RequestBody body = RequestBody.create(JSON, json);
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Response response = client.newCall(request).execute();
            String result;
            if (response.isSuccessful()) {
                result = response.body().string();
            } else {
                result = "Unexpected code";
            }
            refreshUI(result);
        } catch (Exception e) {

        }
    }

    private void OKHttp_Post() {
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

        try {
            String url = et_spec.getText().toString().trim();

            OkHttpClient client = new OkHttpClient();

//        String post(String url, String json) throws IOException {
            String json = new String();
            RequestBody body = RequestBody.create(JSON, json);
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Response response = client.newCall(request).execute();
            String result;
            if (response.isSuccessful()) {
                result = response.body().string();
            } else {
                result = "Unexpected code";
            }
            refreshUI(result);
        } catch (Exception e) {

        }
    }

    private void OKHttpThread_Get_Par() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OKHttp_Get_Par();
            }
        }).start();
    }

    private void OKHttpThread_Get() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OKHttp_Get();
            }
        }).start();
    }

    private void OKHttp_Get_Par() {
        String url = et_spec.getText().toString().trim();


        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .build();

            Response response = null;

            response = client.newCall(request).execute();
            String result;
            if (response.isSuccessful()) {
                result = response.body().string();
            } else {
                result = "Unexpected code";
            }

            refreshUI(result);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     */
    private void OKHttp_Get1() {
//        String url = et_spec.getText().toString().trim();
        String url = "http://www.baidu.com";
        OkHttpManager.getInstance(this).getAsyn(url, new OkHttpCallback() {
            @Override
            public void onError(Call call, Exception e) {
//                Request request = call.request();
//                RequestBody body = request.body();
//                String result = body.toString();
//                Log.d(TAG, "onError result==>"+result);
                Log.d(TAG, "e.toString()==>" + e.toString());
            }

            @Override
            public void onResponse(Response response) {
                ResponseBody body = response.body();
                try {
                    //用这个string打印网页，而不是toString
                    String result = body.string();
                    Log.d(TAG, "onResponse result==>" + result);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     *
     */
    private void OKHttp_Get() {
        String url = et_spec.getText().toString().trim();


        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .build();

            Response response = null;

            response = client.newCall(request).execute();
            String result;
            if (response.isSuccessful()) {
                result = response.body().string();
            } else {
                result = "Unexpected code";
            }

            refreshUI(result);

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private void refreshUI(final String result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(result)) {
                    tv_result.setText(result);
                }
            }
        });
    }
}
